package com.wandian.texas.dao.Impl;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.wandian.texas.GameData;
import com.wandian.texas.dao.UserInfoDao;
import com.wandian.texas.db.DBMgr;
import com.wandian.texas.db.DbParameter;
import com.wandian.texas.db.WandianBaseDao;
import com.wandian.texas.entity.UserInfoEntity;
import com.wandian.util.Log;

/**
 * @author long
 * @Create 2012-10-8 上午9:38:46
 * @QQ 108232706
 * @version 1.0
 * @description UserInfoDaoImpl
 */
public class UserInfoDaoImpl extends WandianBaseDao implements UserInfoDao {

	public UserInfoDaoImpl(DBMgr _dbMgr) {
		super(_dbMgr);
	}

	// @Override
	// protected Connection openConn() {
	// return this.dbMgr.getDbWandian();
	// }

	// protected String dbName(){
	// return DBMgr.
	// }
	public UserInfoEntity getUserInfoById(int id) {
		return null;
	}

	public UserInfoEntity getUserInfo(int userId) {

		String sqlText = "select * from PlayerInfo WHERE  userId=? ;";
		PreparedStatement pstmt = this.execQuery2(sqlText, userId);
		ResultSet rs = null;
		UserInfoEntity info = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if (rs.last()) {
					info = new UserInfoEntity();
					info.setUserId(rs.getInt("UserId"));
					info.setPassword(rs.getString("Password"));
					info.setUserName(rs.getString("userName"));
					info.setNickName(rs.getString("NickName"));
					info.setSex(rs.getByte("Sex"));
					info.setRealName(rs.getString("RealName"));
					info.icon = rs.getString("Icon");
					// info.setCreateDate(new
					// Date(rs.getDate("CreateDate").getTime()));
//					info.setScore(rs.getInt("Score"));
					info.setMoney(rs.getInt("Money"));
					info.rank = rs.getInt("RankMoney");
					info.sumGame = rs.getInt("PlayCount");
					info.winGame = rs.getInt("WinCount");
					info.maxmoney = rs.getInt("MaxMoney");
					info.maxHandCards = rs.getString("MaxHandCard");
					info.maxWin = rs.getInt("MaxWin");
//					info.setGold(rs.getInt("Gold"));
				}
			} catch (SQLException e) {
				info = null;
				Log.error("执行出错" + sqlText, e);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return info;
	}

	String sql_getUserInfo = "select UserId,GameBet,Stake,NickName,HeadImage,Sex,RealName,Icon,RankMoney,PlayCount,WinCount,MaxMoney,MaxHandCard,MaxWin,CurrentState from PlayerInfo WHERE  UserName=? ;";

	public boolean getUserInfo(UserInfoEntity info) {
		String produceName = "sp_GetUserInfo";
		CallableStatement stmt = null;
		this.dbMgr.lock.lock();
		this.dbMgr.setCurrentDb(DBMgr.TexasPokerDB);
		try {
			stmt = this.execProduce2(produceName, info.userName, GameData.SERVER.ServerId);
		} catch (Exception e) {
			return false;
		} finally {
			this.dbMgr.lock.unlock();
		}
		ResultSet rs = null;
		boolean result = false;
		if (stmt != null) {
			try {
				rs = stmt.executeQuery();
				if (rs != null && rs.last()) {
					info.setUserId(rs.getInt("UserId"));
					info.stake = rs.getFloat("Stake");
					info.gameBet = rs.getFloat("GameBet");
					info.setNickName(rs.getString("NickName"));
					info.setHeadImage(rs.getBytes("HeadImage"));
					info.setSex(rs.getByte("Sex"));
					info.setRealName(rs.getString("RealName"));
					info.icon = rs.getString("Icon");
					// info.setCreateDate(new
					// Date(rs.getDate("CreateDate").getTime()));			
					info.rank = rs.getInt("RankMoney");
					info.sumGame = rs.getInt("PlayCount");
					info.winGame = rs.getInt("WinCount");
					info.maxmoney = rs.getFloat("MaxMoney");
					info.maxHandCards = rs.getString("MaxHandCard");
					info.maxWin = rs.getInt("MaxWin");
					info.state = rs.getInt("CurrentState");
					result = true;
				}
			} catch (SQLException e) {
				info = null;
				Log.error("执行出错" + sql_getUserInfo, e);
			} finally {
				closeConn(stmt, rs);
			}
		}
		return result;
	}

	public boolean getUserInfo0(UserInfoEntity info) {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		boolean result = false;
		this.dbMgr.lock.lock();
		try {
			this.dbMgr.setCurrentDb(DBMgr.TexasPokerDB);
			pstmt = this.execQuery2(sql_getUserInfo, info.userName);
			if (pstmt != null) {
				rs = pstmt.executeQuery();
				if (rs != null && rs.last()) {
					info.setUserId(rs.getInt("UserId"));
					info.stake = rs.getFloat("Stake");
					info.gameBet = rs.getFloat("GameBet");
					info.setNickName(rs.getString("NickName"));
					info.setHeadImage(rs.getBytes("HeadImage"));
					info.setSex(rs.getByte("Sex"));
					info.setRealName(rs.getString("RealName"));
					info.icon = rs.getString("Icon");
					// info.setCreateDate(new
					// Date(rs.getDate("CreateDate").getTime()));			
					info.rank = rs.getInt("RankMoney");
					info.sumGame = rs.getInt("PlayCount");
					info.winGame = rs.getInt("WinCount");
					info.maxmoney = rs.getFloat("MaxMoney");
					info.maxHandCards = rs.getString("MaxHandCard");
					info.maxWin = rs.getInt("MaxWin");
					info.state = rs.getInt("CurrentState");
					result = true;
				}
			}
		} catch (SQLException e) {
			info = null;
			Log.error("执行出错" + sql_getUserInfo, e);
		} finally {
			closeConn(pstmt, rs);
			this.dbMgr.lock.unlock();
		}
		return result;
	}

	public UserInfoEntity getUserGameInfo(int userId) {

		String sqlText = "select MaxWin,MaxHandCard,MaxMoney,PlayCount,WinCountm,RankMoney,Money from PlayerInfo WHERE  userId=? ;";
		PreparedStatement pstmt = this.execQuery2(sqlText, userId);
		ResultSet rs = null;
		UserInfoEntity info = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if (rs.last()) {
					info = new UserInfoEntity();
					info.setMoney(rs.getInt("Money"));
					info.rank = rs.getInt("RankMoney");
					info.sumGame = rs.getInt("PlayCount");
					info.winGame = rs.getInt("WinCount");
					info.maxmoney = rs.getInt("MaxMoney");
					info.maxHandCards = rs.getString("MaxHandCard");
					info.maxWin = rs.getInt("MaxWin");
				}
			} catch (SQLException e) {
				info = null;
				Log.error("执行出错" + sqlText, e);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return info;
	}

	public UserInfoEntity getUserInfo(String userName) {
		String sqlText = "select UserId,Password,NickName,HeadImage,Sex,RealName,Icon,Score,Money,RankMoney,PlayCount,WinCount,MaxMoney,MaxHandCard,MaxWin,CurrentState from PlayerInfo WHERE  UserName=? ;";
		PreparedStatement pstmt = this.execQuery2(sqlText, userName);
		ResultSet rs = null;
		UserInfoEntity info = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if (rs.last()) {
					info = new UserInfoEntity();
					info.setUserId(rs.getInt("UserId"));
					info.setPassword(rs.getString("Password"));
					info.setUserName(userName);
					info.setNickName(rs.getString("NickName"));
					info.setHeadImage(rs.getBytes("HeadImage"));
					info.setSex(rs.getByte("Sex"));
					info.setRealName(rs.getString("RealName"));
					info.icon = rs.getString("Icon");
					// info.setCreateDate(new
					// Date(rs.getDate("CreateDate").getTime()));
//					info.setScore(rs.getInt("Score"));
					info.setMoney(rs.getInt("Money"));
					info.rank = rs.getInt("RankMoney");
					info.sumGame = rs.getInt("PlayCount");
					info.winGame = rs.getInt("WinCount");
					info.maxmoney = rs.getInt("MaxMoney");
					info.maxHandCards = rs.getString("MaxHandCard");
					info.maxWin = rs.getInt("MaxWin");
					info.state = rs.getInt("CurrentState");
				}
			} catch (SQLException e) {
				info = null;
				Log.error("执行出错" + sqlText, e);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return info;
	}

//	public boolean addUserInfo(UserInfoEntity info) {
//		boolean result = false;
//		if (info == null) {
//			return result;
//		}
//		if (info.beginAdd()) {
//			String sql = " INSERT INTO PlayerInfo(`UserName`, `NickName`,`Password`, `Sex`, `RealName`, `CreateDate`)  VALUES(?,?, ?, ?, ?, ?);";
//			Map<Integer, DbParameter> paraAdd = new HashMap<Integer, DbParameter>();
//			paraAdd.put(Integer.valueOf(1), new DbParameter(4, info.getUserName()));
//			paraAdd.put(Integer.valueOf(2), new DbParameter(5, info.getNickName()));
//			paraAdd.put(Integer.valueOf(3), new DbParameter(5, info.getPassword()));
//			paraAdd.put(Integer.valueOf(4), new DbParameter(5, info.getSex()));
//			paraAdd.put(Integer.valueOf(5), new DbParameter(5, info.getRealName()));
//			paraAdd.put(Integer.valueOf(6), new DbParameter(5, new Date()));
////			paraAdd.put(Integer.valueOf(7), new DbParameter(5, info.getScore()));
//			result = execNoneQuery(sql, paraAdd) > -1;
//			info.commitAdd(result);
//		}
//		return result;
//	}

	public boolean updateUserInfo(UserInfoEntity info) {
		boolean result = false;
		if (info == null) {
			return result;
		}
		if (info.beginUpdate()) {
			String sql = "  UPDATE PlayerInfo SET  `RealName`=?   Where `UserId`=? And `UserName`=?; ";
			Map<Integer, DbParameter> paraUpdate = new HashMap<Integer, DbParameter>();
			paraUpdate.put(1, new DbParameter(4, info.getRealName()));
			paraUpdate.put(2, new DbParameter(4, info.getUserId()));
			paraUpdate.put(3, new DbParameter(4, info.getUserName()));
			result = execNoneQuery(sql, paraUpdate) > -1;
			info.commitUpdate(result);
		}
		return result;
	}

	public boolean exeSql(String sql, int dbType, Object... params) {
		boolean result = false;
		result = execNoneQuery2(sql, dbType, params) > -1;
		return result;
	}

	public boolean delUserInfo(int userId) {
		boolean result = false;
		String sql = "delete from PlayerInfo where `UserId`=?; ";
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(1, new DbParameter(4, userId));
		result = execNoneQuery(sql, para) > -1;
		return result;
	}

	public boolean delUserInfo(String userName) {
		boolean result = false;
		String sql = "delete from t_userinfo where `UserName`=?; ";
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(1, new DbParameter(4, userName));
		result = execNoneQuery(sql, para) > -1;
		return result;
	}

}
